package com.collect.cameroon.zhiScene.zhiWidget.zhiSubmitRecTagView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.IBinder;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.collect.cameroon.R;
import com.collect.cameroon.zhiBean.zhiSend.ZHISCustomerMarkedOrder;
import com.collect.cameroon.MyApplication;
import com.collect.cameroon.zhiScene.zhiWidget.zhiOptionView.ZHIOption;
import com.collect.cameroon.zhiScene.zhiWidget.zhiOptionView.ZHIOptionView;
import com.collect.cameroon.zhiScene.zhiWidget.zhiStepView.ZHINextStepView;
import com.collect.cameroon.zhiUtils.StringUtil;

import java.util.ArrayList;

public class ZHISubmitRecFollowView extends PopupWindow {

    private View mMenuView;
    private LinearLayout ll_pop_layout;
    private TextView tv_name;
    private TextView tv_contact;
    private TextView tv_call_result;
    private ImageView iv_select_call_result;
    private EditText et_remarks;
    private ZHINextStepView btn_negtive;
    private ZHIOptionView mZHIOptionView;
    private View mParentView;

    private OnSubmitRecFollowItemClickListener mOnSubmitRecFollowItemClickListener;
    private Context mContext;
    private String mOrderId;
    private String mType;
    private String mPhone;

    @SuppressLint("ClickableViewAccessibility")
    public ZHISubmitRecFollowView(Context context) {
        super(context);
        mContext = context;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.popview_record_follow, null);
        ll_pop_layout = mMenuView.findViewById(R.id.ll_pop_layout);
        tv_name = mMenuView.findViewById(R.id.tv_name);
        tv_call_result = mMenuView.findViewById(R.id.tv_call_result);
        iv_select_call_result = mMenuView.findViewById(R.id.iv_select_call_result);
        tv_contact = mMenuView.findViewById(R.id.tv_contact);
        et_remarks = mMenuView.findViewById(R.id.et_remarks);
        btn_negtive = mMenuView.findViewById(R.id.btn_negtive);
        mZHIOptionView = new ZHIOptionView(mContext);

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.PopWindowFromBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xa0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
//        this.setClippingEnabled(false);

        btn_negtive.setOnMenuItemDidChangedListener(item -> {
            if (mOnSubmitRecFollowItemClickListener != null) {
                mOnSubmitRecFollowItemClickListener.onSubmitClick(fillRecFollowParamsV2());
            }
        });

        ll_pop_layout.setOnClickListener(v -> dismissKeyboard(ll_pop_layout));

        iv_select_call_result.setOnClickListener(v -> showOption(""));
        tv_call_result.setOnClickListener(v -> showOption(""));

        mZHIOptionView.setOptionClickListener((type, result) -> {
            try {
                result = StringUtil.safeString(result);
                tv_call_result.setText(result);
                tv_call_result.setTextColor(mContext.getResources().getColor(R.color.theme_black_0));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        mMenuView.setOnTouchListener((v, event) -> {
            int height = mMenuView.findViewById(R.id.ll_pop_layout).getTop();
            int y = (int) event.getY();
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (y < height) {
                    dismiss();
                }
            }
            return true;
        });
    }

    public void setName(String name) {
        try {
            tv_name.setText(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setContact(String contact) {
        try {
            mPhone = contact;
            String contactEncryptFormatter = StringUtil.phoneNumberEncryptFormatter(contact);
            tv_contact.setText(contactEncryptFormatter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setOrderId(String mOrderId) {
        this.mOrderId = mOrderId;
    }

    public void setType(String mType) {
        this.mType = mType;
    }

    public void showOption(String type) {
        try {
            dismissKeyboard(ll_pop_layout);
            if (mZHIOptionView == null) {
                mZHIOptionView = new ZHIOptionView(mContext);
            }
            ArrayList<ZHIOption> arrayList = new ArrayList<>();
            String[] results = {
                    mContext.getResources().getString(R.string.call_result_ptp),
                    mContext.getResources().getString(R.string.call_result_rnr),
                    mContext.getResources().getString(R.string.call_result_fake),
                    mContext.getResources().getString(R.string.call_result_out_of_coverage),
                    mContext.getResources().getString(R.string.call_result_switched_off),
                    mContext.getResources().getString(R.string.call_result_innvalid_num),
                    mContext.getResources().getString(R.string.call_result_msg_sent),
                    mContext.getResources().getString(R.string.call_result_paid),
                    mContext.getResources().getString(R.string.call_result_connected)
            };
            for (int i = 0; i < results.length; i++) {
                ZHIOption option = new ZHIOption();
                option.setTitle(results[i]);
                arrayList.add(option);
            }
            String title = mContext.getString(R.string.select_call_result);
            mZHIOptionView.setOptionTitle(title);
            mZHIOptionView.setOptionType(type);
            mZHIOptionView.setNewData(arrayList);
            mZHIOptionView.showAtLocation(mParentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setCallResult(String result) {
        if (TextUtils.isEmpty(result)) {
            return;
        }
        try {
            tv_call_result.setText(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setmParentView(View parentView) {
        mParentView = parentView;
    }

    public ZHISCustomerMarkedOrder fillRecFollowParamsV2() {
        String callresult = tv_call_result.getText().toString().trim();
        callresult = callresult.startsWith("--") ? "" : callresult;

        ZHISCustomerMarkedOrder zhiSCustomerMarkedOrder = new ZHISCustomerMarkedOrder();
//        zhiSCustomerMarkedOrder.setMobile(tv_contact.getText().toString().trim());
        zhiSCustomerMarkedOrder.setMobile(mPhone);
        zhiSCustomerMarkedOrder.setFullName(tv_name.getText().toString().trim());
        zhiSCustomerMarkedOrder.setRemark(et_remarks.getText().toString().trim());
        zhiSCustomerMarkedOrder.setOrderId(mOrderId);
        zhiSCustomerMarkedOrder.setType(mType);
        zhiSCustomerMarkedOrder.setContactResult(callresult);
        return zhiSCustomerMarkedOrder;
    }

    public void setSubmitRecFollowItemClickListener(OnSubmitRecFollowItemClickListener submitRecTagItemClickListener) {
        this.mOnSubmitRecFollowItemClickListener = submitRecTagItemClickListener;
    }

    public interface OnSubmitRecFollowItemClickListener {
        void onSubmitClick(ZHISCustomerMarkedOrder zhiSCustomerMarkedOrder);
    }

    private void dismissKeyboard(View view) {
        try {
            // 隐藏键盘
            IBinder token = view.getWindowToken();
            InputMethodManager inputMethodManager = (InputMethodManager) MyApplication.getAppContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(token,
                    InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
